//
//  GRReturnGoodView.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/7/24.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRReturnGoodView.h"
@interface GRReturnGoodView()
@property (weak, nonatomic) IBOutlet UIImageView *goodImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *specLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLable;
@property (weak, nonatomic) IBOutlet UILabel *originLabel;
@property(nonatomic,assign)NSInteger GoodCount;
@property(nonatomic,weak)id <GRReturnGoodViewDelegate> delegate;
@property (weak, nonatomic) IBOutlet UITextField *countTF;
@property(nonatomic,assign)NSInteger maxCount;


@end

@implementation GRReturnGoodView


+(instancetype)shareInstance{
    static GRReturnGoodView * returnGoodView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        returnGoodView = [GRReturnGoodView xx_instantiateFromNib];
    });
    return returnGoodView;
}

+(void)showGoodView:(id <GRReturnGoodViewDelegate>) delegate MaxCount:(NSInteger)maxCount{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    GRReturnGoodView  * goodView = [GRReturnGoodView shareInstance];
    goodView.delegate = delegate;
    goodView.GoodCount  = 1;
    goodView.countTF.text = @"1";
    goodView.maxCount = maxCount;
    [window addSubview:goodView];
    [goodView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(window);
    }];
}
- (IBAction)addAction:(id)sender {
    [self addOrReduceGood:1];
}

- (IBAction)reduceAction:(id)sender {
    [self addOrReduceGood:-1];
}

- (IBAction)confimAction:(id)sender {
    [self removeFromSuperview];
    if (_delegate && [_delegate respondsToSelector:@selector(returnGoodQuality:)]) {
        [_delegate returnGoodQuality:_GoodCount];
    }
   
}

-(void)addOrReduceGood:(NSInteger)count{
    _GoodCount = _GoodCount+count;
    if (_GoodCount <=0) {
        _GoodCount = 1;
    }
    if (_GoodCount >=_maxCount ) {
        _GoodCount =_maxCount;
    }
    self.countTF.text = [NSString stringWithFormat:@"%ld",_GoodCount];
    
}


@end
